const { read, write } = require('../lib/read&write')

const NOTICEURL = '../data/notice.json'
function setNotice(notice) {
  const __Notice = read(NOTICEURL)
  __Notice.push(notice)
  write(__Notice, NOTICEURL)
}

function editNotice(notice) {
  const __Notice = read(NOTICEURL)
  __Notice.splice(
    __Notice.findIndex((item) => item.content == notice.content),
    1,
    notice,
  )
  write(__Notice, NOTICEURL)
}

function deleteNotice(content) {
  const __Notice = read(NOTICEURL)
  __Notice.splice(
    __Notice.findIndex((item) => item.content == content),
    1
  )
  write(__Notice, NOTICEURL)
}

function getAllNotices() {
  const __Notice = read(NOTICEURL)
  return __Notice
}

function searchNotice(content) {
  const __Notice = read(NOTICEURL)
  return __Notice.find((item) => item.content === content)
}

module.exports = {
  searchNotice,
  getAllNotices,
  editNotice,
  setNotice,
  // 以下未用
  deleteNotice,
}
